package com.atXiao.food.controller;

import com.atXiao.food.aspect.annotation.LogAspect;
import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.reqbody.ReqBody;
import com.atXiao.food.reqbody.receipt.ReceiptDetailReq;
import com.atXiao.food.reqbody.receipt.ReceiptIconReq;
import com.atXiao.food.rspbody.RspBody;
import com.atXiao.food.rspbody.category.CategoryItem;
import com.atXiao.food.rspbody.recipe.ReceiptDetail;
import com.atXiao.food.rspbody.recipe.ReceiptIcon;
import com.atXiao.food.rspcode.RspResultCode;
import com.atXiao.food.service.ReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: lixiaoxiao
 * @date: 2023/11/23 14:35
 * @version: 1.0
 * @desc:
 */
@RestController
@RequestMapping(path = "/receipt", produces = MediaType.APPLICATION_JSON_VALUE)
public class ReceiptController {
    public final static Log logger = new Log(ReceiptController.class);

    @Autowired
    ReceiptService receiptService;


    /**
     * 菜谱Icon列表查询
     * @param trace
     * @param reqBody
     * @return
     */
    @PostMapping(path = "/iconQuery")
    @ResponseBody
    @LogAspect(busSign = BusSignConstant.TASTE_FOOD, busCode = BuscodeConstant.CATEGORY_ICON_QUERY)
    public RspBody<List<ReceiptIcon>> queryIconList(@RequestHeader(name = "Trace") String trace, @Valid @RequestBody ReqBody<ReceiptIconReq> reqBody) {
        RspBody<List<ReceiptIcon>> rspBody = new RspBody();
        try {
            List<ReceiptIcon> iconList = receiptService.queryReceiptIconList(trace, reqBody.getReqBody());
            rspBody.setRetCode(RspResultCode.REQUEST_SUCCESS.getCode());
            rspBody.setRetDesc(RspResultCode.REQUEST_SUCCESS.getDesc());
            rspBody.setRspBody(iconList);
        } catch (Exception e) {
            logger.error(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.CATEGORY_ICON_QUERY, e.getMessage(), e);
            rspBody = RspBody.fail(null);
        }
        return rspBody;
    }


    /**
     * 菜谱详情
     * @param trace
     * @param reqBody
     * @return
     */
    @PostMapping(path = "/detail")
    @ResponseBody
    @LogAspect(busSign = BusSignConstant.TASTE_FOOD, busCode = BuscodeConstant.CATEGORY_DETAIL_QUERY)
    public RspBody<ReceiptDetail> queryReceiptDetail(@RequestHeader(name = "Trace") String trace, @Valid @RequestBody ReqBody<ReceiptDetailReq> reqBody) {
        RspBody<ReceiptDetail> rspBody = new RspBody();
        try {
            ReceiptDetail receiptDetail = receiptService.queryReceiptDetailById(trace, reqBody.getReqBody());
            rspBody.setRetCode(RspResultCode.REQUEST_SUCCESS.getCode());
            rspBody.setRetDesc(RspResultCode.REQUEST_SUCCESS.getDesc());
            rspBody.setRspBody(receiptDetail);
        } catch (Exception e) {
            logger.error(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.CATEGORY_DETAIL_QUERY, e.getMessage(), e);
            rspBody = RspBody.fail(null);
        }
        return rspBody;
    }



}
